package video.local;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import exception.VideoNotFoundException;

import settings.GlobalSettings;
import video.local.analyse.image.ImageSimilarity;
import video.local.analyse.image.MergedImage;
import video.web.UrlParser;

/**
 * basic methods of class video
 * @author ladjzero
 *
 */
public class VideoBasicMethodsSet {
	/**
	 * 
	 * @param videoFilePath
	 * @param captureFolder
	 * @return image files list
	 */
	public static File[] capture(String videoFilePath, String captureFolder){
		return capture(videoFilePath, captureFolder, false , 1.0);
	}
	/**
	 * 
	 * @param videoFilePath
	 * @param captureFolder
	 * @param removeRepeat
	 * @param repeatThreshold
	 * @return image files list
	 */
	public static File[] capture(String videoFilePath, String captureFolder, boolean removeRepeat, double repeatThreshold){
		//clean test folder
		File captureDir = new File(GlobalSettings.stereoTestFolder);
		File[] images = captureDir.listFiles();
		for(int i=0; i<images.length; i++)
			images[i].delete();
		
		//capture screenshot
		String cmd = "ffmpeg -loglevel quiet -i \"" + videoFilePath	+ "\" -r 1 -s 320x240 -f image2 " + captureFolder + "\\%d.jpg";
		Process capture;
		try {
			capture = Runtime.getRuntime().exec(cmd);
			capture.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//remove repeat
		if(removeRepeat)
			removeRepeats(GlobalSettings.stereoTestFolder, repeatThreshold);
		
		//to return
		File[] imageList = new File(captureFolder).listFiles(new FilenameFilter(){
			public boolean accept(File dir, String name) {
				if( name.endsWith(".jpg"))
					return true;
				return false;
			}	
		});
				
		return imageList;
	}
	/**
	 * 
	 * @param imageFolder
	 * @param repeatThreshold
	 */
	public static void removeRepeats(String imageFolder, double repeatThreshold){
		//how many images in total?
		int imageNum = new File(imageFolder).listFiles(new FilenameFilter(){
			public boolean accept(File dir, String name) {
				if( name.endsWith(".jpg"))
					return true;
				return false;
			}	
		}).length;
		
		//imageList index increases with index of image file!
		//by default, image index will go as "1, 10, 11 ... 100 ... 2, 20 ..."
		//i need it go as "1 2 3 4 ..."
		File[] imageList = new File[imageNum];
		for(int i=0; i<imageNum; i++)
			imageList[i] = new File(imageFolder+File.separator+(i+1)+".jpg");
		
		//delete repeats
		for(int i=0; i<imageList.length; i++){
			File image1 = imageList[i];	//because i is changing in loop
			for(int j=i+1; j<imageList.length; j++){		
				File image2 = imageList[j];
				if(imageList[j].exists()){
					System.out.println(image1.getPath()+"\t"+image2.getPath());
					try {
						//if repeat, then delete
						if( ImageSimilarity.getSimilarity(image1, image2) >= repeatThreshold){
							imageList[j].delete();
							i++; //let i skip this index (j)
							System.out.println(imageList[j].getPath()+" delete.");
						}
						else
							break;
						//why break? i think the repeats are adjacent, limited in a specified interval.
					} catch (IOException e) {
						System.out.println(imageList[i].getPath()+"\tor\t"+imageList[j].getPath()+"\tnot found");
					}
				}
			}
		}
	}

	/**
	 * 
	 * @param video url
	 * @return video path
	 * @throws IOException
	 */
	public static String getVideoPathByVideoPageUrl(String videoUrl) throws VideoNotFoundException{
		File siteDir = new File( GlobalSettings.videoFileFolder+"\\"+UrlParser.getSiteDomain(videoUrl) );
		for(int i=0; i<siteDir.list().length; i++){
			if( siteDir.list()[i].contains(UrlParser.getVid(videoUrl)))
				return GlobalSettings.videoFileFolder+"\\"+UrlParser.getSiteDomain(videoUrl)+"\\"+siteDir.list()[i];
		}
		
		throw new VideoNotFoundException(videoUrl+"\tVideo file not found.");
	}
}
